package cn.yuanqiao.web.controller.activiti;

import cn.yuanqiao.activiti.domain.ActHistoryTaskDTO;
import cn.yuanqiao.activiti.domain.ActTaskDTO;
import cn.yuanqiao.activiti.service.IActTaskService;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.PageDomain;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.core.page.TableSupport;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.github.pagehelper.Page;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 归档流程信息Controller
 * 
 * @author lyk
 * @date 2023-07-28
 */
@RestController
@RequestMapping("/activiti/workflowArchiveBorrow")
public class YqdaWorkflowArchiveBorrowController extends BaseController
{

    @Autowired
    private IActTaskService actTaskService;

    @Autowired
    private RuntimeService runtimeService;



    /**
     * bpmn流程图片地址
     */
    @Value("${bpmn.domain}")
    public String domain;


    //获取我的代办任务
    @GetMapping(value = "/taskList")
    public TableDataInfo getTasks(@RequestParam(value = "instanceName",required = false) String instanceName) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ActTaskDTO> hashMaps = actTaskService.selectProcessDefinitionListByCode(pageDomain,"WorkFlow_Archive_Borrow",instanceName);
        return getDataTable(hashMaps);
    }

    //获取我的代办任务
    @GetMapping(value = "/taskHistoryList")
    public TableDataInfo getTaskHistoryList(@RequestParam(value = "instanceName",required = false) String instanceName) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ActHistoryTaskDTO> hashMaps = actTaskService.selectProcessDefinitionListHistoyByCode(pageDomain,"WorkFlow_Archive_Borrow",instanceName);
        return getDataTable(hashMaps);
    }

    @Log(title = "归档流程信息", businessType = BusinessType.DELETE)
    @PutMapping("/setBorrowProcessDownloadStatus/{processId}/{downloadNum}/{allowDownload}")
    public AjaxResult setArchiveProcessStatusByArchiveId(@PathVariable("processId") String processId,
                                                         @PathVariable("downloadNum") String downloadNum,
                                                         @PathVariable("allowDownload") String allowDownload)
    {
        Map<String,Object> variable = (Map<String,Object>)runtimeService.getVariable(processId, "processVariables");
        variable.put("allowDownload",allowDownload);
        variable.put("downloadNum",downloadNum);
        runtimeService.setVariable(processId,"processVariables",variable);
        //runtimeService.setVariables(processId);
        return AjaxResult.success("状态值修改成功");
    }

    @Log(title = "借阅流程信息", businessType = BusinessType.UPDATE)
    @PutMapping("/setBorrowProcessPrintStatus/{processId}/{printNum}/{allowPrint}")
    public AjaxResult setBorrowProcessPrintStatus(@PathVariable("processId") String processId,
                                                  @PathVariable("printNum") String printNum,
                                                  @PathVariable("allowPrint") String allowPrint)
    {
        Map<String,Object> variable = (Map<String,Object>)runtimeService.getVariable(processId, "processVariables");
        variable.put("allowPrint",allowPrint);
        variable.put("printNum",printNum);
        runtimeService.setVariable(processId,"processVariables",variable);
        //runtimeService.setVariables(processId);
        return AjaxResult.success("状态值修改成功");
    }

}
